package com.xhwl.logistics.service.cent;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.common.enums.CategoryEnum;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import com.xhwl.common.query.cent.device.ProductAccessQuery;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigurationBO;
import com.xhwl.logistics.bo.cent.VehicleParkingInfoBO;
import com.xhwl.logistics.converter.mapstruct.VehicleIntegratedConfigConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Slf4j
//@Service
public class QueryDeviceService {

    @Autowired
    private IProductAccessCentService productAccessCentService;

    @Autowired
    private VehicleIntegratedConfigConverter vehicleIntegratedConfigConverter;
    @Autowired
    private QueryDeviceService queryDeviceService;

    public Integer queryProjectIdFromIntegratedConfiguration(String parkId) {

        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setJsonFieldName("thirdParkId");
        productAccessQuery.setJsonFieldValue(parkId);

        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = productAccessCentService.listProductAccess(null, productAccessQuery);

        if (deviceProjSupplierCategoryList.size()==1) {
            return deviceProjSupplierCategoryList.get(0).getProjectId();
        } else {
            throw new BusinessException("通过车场id找到多个项目或者找不到项目");
        }
    }

    @Deprecated   //(已被queryIntegratedConfigFromProjectId替换)
    public VehicleIntegratedConfigurationBO queryIntegratedConfigurationFromProjectId(Integer projectId) {
        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setProjectId(projectId);
        productAccessQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());

        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = productAccessCentService.listProductAccess(null, productAccessQuery);

        int size = deviceProjSupplierCategoryList.size();
        if (size > 1) {
            throw new BusinessException("通过项目id找到多个车场集成配置");
        }
        if (size < 1) {
            throw new BusinessException("通过项目id找不到车场集成配置");
        }

        DeviceProjSupplierCategory deviceProjSupplierCategory = deviceProjSupplierCategoryList.get(0);
        JSONObject properties = deviceProjSupplierCategory.getProperties();

        VehicleIntegratedConfigurationBO vehicleIntegratedConfigurationBO = properties.toJavaObject(VehicleIntegratedConfigurationBO.class);

        log.info("vehicleIntegratedConfigurationBO: " + vehicleIntegratedConfigurationBO);

        return vehicleIntegratedConfigurationBO;
    }

    public VehicleIntegratedConfigBO queryIntegratedConfigFromProjectId(Integer projectId) {
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = new VehicleIntegratedConfigBO();

        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setProjectId(projectId);
        productAccessQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());

        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = productAccessCentService.listProductAccess(null, productAccessQuery);
        int size = deviceProjSupplierCategoryList.size();
        if (size > 1) {
            throw new BusinessException("通过项目id找到多个车场集成配置");
        }
        if (size < 1) {
            throw new BusinessException("通过项目id找不到车场集成配置");
        }

        DeviceProjSupplierCategory deviceProjSupplierCategory = deviceProjSupplierCategoryList.get(0);
        vehicleIntegratedConfigBO.setVehicleSystemInfoBO(vehicleIntegratedConfigConverter.deviceProjSupplierCategory2VehicleSystemInfoBO(deviceProjSupplierCategory));

        JSONObject jsonProperties = deviceProjSupplierCategory.getProperties();
        if (null!=jsonProperties) {
            vehicleIntegratedConfigBO.setVehicleParkingInfoBO(jsonProperties.toJavaObject(VehicleParkingInfoBO.class));
        }

        return vehicleIntegratedConfigBO;
    }

    public VehicleIntegratedConfigBO queryIntegratedConfigFromParkId(String parkId) {
        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setJsonFieldName("thirdParkId");
        productAccessQuery.setJsonFieldValue(parkId);
        productAccessQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());

        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = productAccessCentService.listProductAccess(null, productAccessQuery);

        int size = deviceProjSupplierCategoryList.size();
        if (size > 1) {
            throw new BusinessException("通过车场id找到多个车场集成配置");
        }
        if (size < 1) {
            throw new BusinessException("通过车场id找不到车场集成配置");
        }

        DeviceProjSupplierCategory deviceProjSupplierCategory = deviceProjSupplierCategoryList.get(0);
        return vehicleIntegratedConfigConverter.deviceProjSupplierCategory2VehicleIntegratedConfigBO(deviceProjSupplierCategory);
    }

    public List<VehicleIntegratedConfigBO> queryIntegratedConfigs() {
        ProductAccessQuery productAccessQuery = new ProductAccessQuery();
        productAccessQuery.setCategoryId(CategoryEnum.YARD_GATE.getId());

        List<DeviceProjSupplierCategory> deviceProjSupplierCategoryList = productAccessCentService.listProductAccess(null, productAccessQuery);
        return vehicleIntegratedConfigConverter.deviceProjSupplierCategoryList2VehicleIntegratedConfigBOList(deviceProjSupplierCategoryList);
    }

    public String queryAppsecretFromParkId(String parkId){
        VehicleIntegratedConfigBO vehicleIntegratedConfigBO = queryDeviceService.queryIntegratedConfigFromParkId(parkId);
        VehicleParkingInfoBO vehicleParkingInfoBO = vehicleIntegratedConfigBO.getVehicleParkingInfoBO();
        return vehicleParkingInfoBO.getAppSecret();
    }
}
